package com.dlc.shop.systemtest.api.amount;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dlc.shop.bean.app.dto.OrderNumbersDto;
import com.dlc.shop.bean.app.param.SubmitSeckillOrderParam;
import com.dlc.shop.bean.model.OfflineHandleEvent;
import com.dlc.shop.bean.param.OfflineHandleEventAuditParam;
import com.dlc.shop.common.util.Json;
import com.dlc.shop.seckill.api.param.SeckillOrderParam;
import com.dlc.shop.seckill.common.model.Seckill;
import com.dlc.shop.seckill.multishop.param.SeckillParam;
import com.dlc.shop.systemtest.constants.Constant;
import com.dlc.shop.systemtest.util.HttpUtil;

import java.util.List;

/**
 * @author lanhai
 */
public class SeckillApi {
    /**
     * 新建秒杀活动
     * @param seckillParam
     */
    public static void createSeckill(SeckillParam seckillParam){
        String url = Constant.SHOP_URL+"/seckill/seckill";
        HttpUtil.post(url,seckillParam);
    }

    /**
     * 获取秒杀活动列表
     */
    public static List<Seckill> listSeckill() {
        String url = Constant.SHOP_URL + "/seckill/seckill/page";
        return JSON.parseArray(JSON.toJSONString(Json.parseObject(JSON.toJSONString(HttpUtil.get(url, Page.class)), Page.class).getRecords()), Seckill.class);
    }

    /**
     * 平台违规下架秒杀活动
     */
    public static void offine(OfflineHandleEvent offlineHandleEvent) {
        String url = Constant.PLATFORM_URL + "/platform/seckill/offline";
        HttpUtil.post(url, offlineHandleEvent);
    }

    /**
     * 商家根据活动ID获取下架信息
     */
    public static OfflineHandleEvent getOfflineHandleEvent(Long seckillId) {
        String url = Constant.SHOP_URL + "/seckill/seckill/getOfflineHandleEventBySeckillId/" + seckillId;
        return HttpUtil.get(url, OfflineHandleEvent.class);
    }

    /**
     * 商家根据活动下架信息申请上架
     */
    public static void applyOnline(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        String url = Constant.SHOP_URL + "/seckill/seckill/auditApply";
        HttpUtil.post(url, offlineHandleEventAuditParam);
    }

    /**
     * 平台根据活动ID获取下架信息
     */
    public static OfflineHandleEvent getOfflineHandleEventBySeckillId(Long seckillId) {
        String url = Constant.PLATFORM_URL + "/platform/seckill/getOfflineHandleEventBySeckillId/" + seckillId;
        return HttpUtil.get(url, OfflineHandleEvent.class);
    }

    /**
     * 平台根据活动下架信息审核
     */
    public static void audit(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        String url = Constant.PLATFORM_URL + "/platform/seckill/auditSeckill";
        HttpUtil.post(url, offlineHandleEventAuditParam);
    }


    /**
     * 店铺关闭秒杀活动
     */
    public static void closeSeckill(Long seckillId) {
        String url = Constant.SHOP_URL + "/seckill/seckill/invalid/" + seckillId;
        HttpUtil.put(url, seckillId);
    }

    /**
     * 店铺删除秒杀活动
     * @param seckillId
     */
    public static void shopDeleteSeckill(Long seckillId){
        String url = Constant.SHOP_URL+"/seckill/seckill/"+seckillId;
        HttpUtil.delete(url);
    }

    /**
     * 平台删除秒杀活动
     * @param seckllId
     */
    public static void deleteSeckill(Long seckllId){
        String url = Constant.PLATFORM_URL+"/seckill/seckill/"+seckllId;
        HttpUtil.delete(url);
    }


    /**
     * 用户根据商品ID获取秒杀订单路径
     */
    public static String getSeckillPath(Long productId) {
        String url = Constant.API_URL + "/p/seckill/orderPath?prodId=" + productId;
        return HttpUtil.get(url, String.class);
    }

    /**
     * 用户根据商品ID购买秒杀商品
     */
    public static void buySeckill(SeckillOrderParam seckillOrderParam, String path) {
        String url = Constant.API_URL + "/p/seckill/" + path + "/confirm";
        HttpUtil.post(url, seckillOrderParam);
    }

    /**
     * 提交秒杀订单
     */
    public static OrderNumbersDto submitSeckillOrder(String path, SubmitSeckillOrderParam submitSeckillOrderParam) {
        String url = Constant.API_URL + "/p/seckill/" + path + "/submit";
        return HttpUtil.post(url, submitSeckillOrderParam, OrderNumbersDto.class);
    }
}
